var game = null;
var SPACE = 32;

function Game(length, recorder) {
    this.bars = "";
    this.word = this.randWord(length);
    this.wordLength = this.word.length;
    this.pointsCounter = this.word.length;
    this.errorsCounter = 11;
    this.recorder = recorder;
    this.comunicatesDiv = $("#comunicates");
    for (var i = 0; i < this.wordLength; i++) {
	this.bars += "X";
    }
    game = this;

    $(window).keydown(function(e) {
	if (e.keyCode == SPACE) {
	    game.startGuessing();
	}
    });

    $(window).keyup(function(e) {
	if (e.keyCode == SPACE) {
	    game.endGuessing();
	}
    });

    this.drawBars();

    $("#gallows").hide().html('<img src="gfx/' + this.errorsCounter + '.jpg">')
	    .fadeIn('fast');

}

Game.prototype.guess = function(letter) {

    console.log("GUSEEING: [" + letter + "]");
    if (this.word.indexOf(letter) != -1) {
	if (this.bars.indexOf(letter) != -1) {
	    this.comunicatesDiv.html('Litera <span class="ltr">' + letter + '</span> została użyta wcześniej.');
	} else {
	    this.comunicatesDiv.html('Litera <span class="ltr">' + letter + '</span> odgadnięta!');
	    this.pointsCounter--;
	    var text = "";
	    for (var i = 0; i < this.wordLength; i++) {
		if (this.word.charAt(i) == letter) {
		    text += letter;
		} else {
		    text += this.bars.charAt(i);
		}
	    }
	    this.bars = text;
	}
    } else {
	this.comunicatesDiv.html('Niestety... <span class="ltr">' + letter + '</span> to pudło!');
	this.errorsCounter--;
    }

    if (this.errorsCounter <= 0) {
	this.comunicatesDiv.html("Wisisz! Tym razem się nie udało...");
	this.freeze();
    }

    else if (this.pointsCounter <= 0) {
	this.comunicatesDiv.html("Udało się odgadnąć słowo! Gratulacje!");
	this.freeze();
    }

    this.drawBars();

    $("#gallows img").attr('src', 'gfx/' + this.errorsCounter + '.jpg');
}

Game.prototype.startGuessing = function() {
    this.recorder.startRecording();
}

Game.prototype.endGuessing = function() {
    this.recorder.stopRecording();
    this.guess(this.recorder.lastCharacter.trim());

}

Game.prototype.freeze = function() {
    $(window).off('keydown');
    $(window).off('keyup');
    $("#instructions").html("Koniec gry. <b>Kilknij tutaj</b>, by zagrać ponownie.");
    $("#instructions").click(function(e) {
	location.reload();
    });
}

Game.prototype.randWord = function(length) {
    
    var words = new Array();
    
    words.push("TERAKOTA");
    words.push("KATAPULTA");
    words.push("MAKARON");
    words.push("KARUZELA");
    words.push("KARAFKA");
    words.push("KORALE");
    words.push("KRYKIET");
    words.push("KARNACJA");
    words.push("PRZERWANIE");
    words.push("MARAZM");
    words.push("MARAKASY");
    words.push("MARIONETKA");
    words.push("MARTWICA");
    words.push("BUTELKA");
    words.push("KLAWIATURA");
    words.push("FORTEPIAN");
    
    var number = Math.floor(Math.random() * words.length);
    
    return words[number];
}

Game.prototype.drawBars = function() {
    var html = '';
    for (var i = 0; i < this.wordLength; i++) {
	var char = this.bars.charAt(i);
	if(char == 'X') {
	    char = '.';
	}
	html += '<span class="letter">' + char + '</span>';
    }
    $("#bars").html(html);
}